/**
 * 
 */
package com.netscenery.yang.model;

import org.jdom2.Attribute;
import org.jdom2.Element;

import com.netscenery.util.ErrTag;
import com.netscenery.util.Unicode;
import com.netscenery.util.XMLTag;
import com.netscenery.yang.YangKeyword;
import com.netscenery.yang.YangStatement;
import com.netscenery.yang.designer.MapInfo;

/**
 * @author llly
 *
 */
public class Enum extends ModelElement {
	private int value;
	private boolean isUseDefaultValue = true;
	private int actualValue;
	private String  description;
	private Status status = Status.CURRENT;
	private String reference;
	
	
	
	public Enum(String name){
		setName(name);
		isUseDefaultValue = true;
	}
	
	public Enum(String name,int value){
		setName(name);
		this.value = value;
		this.isUseDefaultValue = false;
	}
	
	public boolean checkName(String name){
		return true;
	}
	
	public boolean setName(String name){
		return super.setName(name.trim());
	}

	public String getReference() {
		return reference;
	}

	public void setReference(String reference) {
		this.reference = reference;
	}

	/**
	 * @return the actualValue
	 */
	public int getActualValue() {
		return actualValue;
	}

	/**
	 * @param actualValue the actualValue to set
	 */
	public void setActualValue(int actualValue) {
		this.actualValue = actualValue;
	}

	/**
	 * @return the value
	 */
	public int getValue() {
		return value;
	}

	/**
	 * @param value the value to set
	 */
	public void setValue(int value) {
		this.value = value;
		this.isUseDefaultValue = false;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the status
	 */
	public Status getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(Status status) {
		if (null == status){
			return;
		}
		this.status = status;
	}
	
	

	/**
	 * @return the isUseDefaultValue
	 */
	public boolean isUseDefaultValue() {
		return isUseDefaultValue;
	}



	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#validate()
	 */
	@Override
	public Validator validate() {
		Validator validator = new Validator();
		
		if (isNullString(name) ){
			validator.setIsValid(false);
			validator.addMsg(new Message(this,ErrTag.ERROR,ErrorCode.COMMON_INVALIDNAME));
		}
		if (this.value < -2147483648 
				|| this.value > 2147483647){
			validator.setIsValid(false);
			validator.addMsg(new Message(this,ErrTag.ERROR,"value",ErrorCode.INVALID_VALUE));

		}
		if (!isNullString(this.description)){
			if (Unicode.containChinese(this.description)){
				validator.setIsValid(false);
				validator.addMsg(new Message(this,ErrTag.ERROR,"description",ErrorCode.INVALID_CHARACTER));
			}
		}
		
		if (!isNullString(this.reference)){
			if (Unicode.containChinese(this.reference)){
				validator.setIsValid(false);
				validator.addMsg(new Message(this,ErrTag.ERROR,"reference",ErrorCode.INVALID_CHARACTER));
			}
		}
		
		Validator sValidator = super.validate();
		validator.copy(sValidator);
		return validator;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#toYang()
	 */
	@Override
	public YangStatement toYang() {

		YangStatement enumStatement = new YangStatement(YangStatement.YANG_STATEMENT_ENUM,this.name);
		if (false == this.isUseDefaultValue){
			YangStatement value = new YangStatement(YangStatement.YANG_STATEMENT_VALUE,new Integer(this.value).toString());
			enumStatement.addChild(value);
		}

		
		if (null != this.description){
			YangStatement descr = new YangStatement(YangStatement.YANG_STATEMENT_DESCRIPTION,this.description);
			enumStatement.addChild(descr);
		}
		if (null != this.reference){
			YangStatement descr = new YangStatement(YangStatement.YANG_STATEMENT_REFERENCE,this.reference);
			enumStatement.addChild(descr);
		}

		if (!Status.CURRENT.equals(this.status)) {
			YangStatement status = new YangStatement(
					YangStatement.YANG_STATEMENT_STATUS,
					this.status.getStatus());
			enumStatement.addChild(status);
		}
			

		return enumStatement;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#toXML()
	 */
	@Override
	public Element toXML() {
	
		Element enumElement = new Element(XMLTag.XML_TAG_ENUM);
		Element name = new Element(XMLTag.XML_TAG_NAME);
		name.setText(this.name);
		enumElement.addContent(name);
//		if (false == this.isUseDefaultValue){
//			Element value = new Element(XMLTag.XML_TAG_VALUE);
//			value.setText(new Integer(this.value).toString());
//			enumElement.addContent(value);
//		}
		Element value = new Element(XMLTag.XML_TAG_VALUE);
		value.setText(new Integer(this.getActualValue()).toString());
		enumElement.addContent(value);
		if (null != this.description){
			Element descr = new Element(XMLTag.XML_TAG_DESCRIPTION);
			descr.setText(this.description);
			enumElement.addContent(descr);
		}
		if (null != this.reference){
			Element ref = new Element(XMLTag.XML_TAG_REFERENCE);
			ref.setText(this.reference);
			enumElement.addContent(ref);
		}
		
//		if (null != this.status){
//			if (!Status.DEFAULT.equals(this.status)){
//				Element status = new Element(XMLTag.XML_TAG_STATUS);
//				status.setText(this.status.getStatus());
//				enumElement.addContent(status);
//			}
//			
//		}
		
		Element status = new Element(XMLTag.XML_TAG_STATUS);
		status.setText(this.status.getStatus());
		enumElement.addContent(status);
		return enumElement;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#isFieldMandatory(java.lang.String)
	 */
	@Override
	public boolean isFieldMandatory(String field) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readXML(org.jdom2.Element)
	 */
	@Override
	public int readXML(Element element) {
		if (null == element){
			return MODELELEMENT_OK;
		}
		

		String value = element.getText();
		if (element.getName().equals(XMLTag.XML_TAG_VALUE)){
			if (null == value){
				this.isUseDefaultValue = true;
			}
			else {
				this.isUseDefaultValue = false;
				this.value = new Integer(value).intValue();
			}
		}
		else if (element.getName().equals(XMLTag.XML_TAG_DESCRIPTION)){
			this.description = value;
		}
		else if (element.getName().equals(XMLTag.XML_TAG_REFERENCE)){
			this.reference = value;
		}
		else if (element.getName().equals(XMLTag.XML_TAG_STATUS)){
			this.status = Status.getStatus(value);
		}
		else if (element.getName().equals(XMLTag.XML_TAG_NAME)){
			return MODELELEMENT_OK;
		}
		else {
			return MODELELEMENT_FAIL;
		}
		
		return MODELELEMENT_OK;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readYANG(com.netscenery.modeldesigner.util.YangStatement)
	 */
	@Override
	public int readYANG(YangStatement statement) {
		if (null == statement){
			return MODELELEMENT_OK;
		}
		String value = statement.getArgument();
		if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_VALUE)){
			if (null == value){
				this.isUseDefaultValue = true;
			}
			else {
				this.isUseDefaultValue = false;
				this.value = new Integer(value).intValue();
			}
		}
		else if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_DESCRIPTION)){
			this.description = value;
		}
		else if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_REFERENCE)){
			this.reference = value;
		}
		else if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_STATUS)){
			this.status = Status.getStatus(value);
		}
		else {
			return MODELELEMENT_FAIL;
		}
		
		return MODELELEMENT_OK;
	}

	@Override
	public MapInfo getMapInfo() throws SecurityException, NoSuchMethodException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Element toYin() {
		Element enumStatement = new Element(YangStatement.YANG_STATEMENT_ENUM,env.getNs());
		Attribute name = new Attribute(XMLTag.XML_TAG_NAME,this.name);
		enumStatement.setAttribute(name);
		if (false == this.isUseDefaultValue){
			Element value = new Element(YangStatement.YANG_STATEMENT_VALUE,env.getNs());
			Attribute aValue = new Attribute(YangKeyword.VALUE.getKeyword(),
					new Integer(this.value).toString());
			value.setAttribute(aValue);
			enumStatement.addContent(value);
		}

		
		if (null != this.description){
			Element descr = new Element(YangStatement.YANG_STATEMENT_DESCRIPTION,env.getNs());
			Element text = new Element(XMLTag.XML_TAG_TEXT,env.getNs());
			text.setText(this.description);
			descr.addContent(text);
			enumStatement.addContent(descr);
		}
		if (null != this.reference){
			Element ref = new Element(YangStatement.YANG_STATEMENT_REFERENCE,env.getNs());
			Element text = new Element(XMLTag.XML_TAG_TEXT,env.getNs());
			text.setText(this.reference);
			ref.addContent(text);
			enumStatement.addContent(ref);
		}

		if (!Status.CURRENT.equals(this.status)) {
			Element status = new Element(YangStatement.YANG_STATEMENT_STATUS,env.getNs());
			Attribute value = new Attribute(YangKeyword.VALUE.getKeyword(),this.getStatus().getStatus());
			status.setAttribute(value);
			enumStatement.addContent(status);
		}
			

		return enumStatement;
	}

	@Override
	public int readYin(Element statement) throws ModelException {
		if (null == statement){
			return MODELELEMENT_OK;
		}
		
		if (statement.getName().equals(YangStatement.YANG_STATEMENT_VALUE)){
			String value = statement.getAttributeValue(YangKeyword.VALUE.getKeyword());
			if (null == value){
				this.isUseDefaultValue = true;
			}
			else {
				this.isUseDefaultValue = false;
				this.value = new Integer(value).intValue();
			}
		}
		else if (statement.getName().equals(YangStatement.YANG_STATEMENT_DESCRIPTION)){
			Element text = statement.getChild(XMLTag.XML_TAG_TEXT,env.getNs());
			if (null == text){
				return MODELELEMENT_FAIL;
			}
			this.setDescription(text.getText());
		}
		else if (statement.getName().equals(YangStatement.YANG_STATEMENT_REFERENCE)){
			Element text = statement.getChild(XMLTag.XML_TAG_TEXT,env.getNs());
			if (null == text){
				return MODELELEMENT_FAIL;
			}
			this.setReference(text.getText());
		}
		else if (statement.getName().equals(YangStatement.YANG_STATEMENT_STATUS)){
			String value= statement.getAttributeValue(YangKeyword.VALUE.getKeyword());
			if (null == value){
				return MODELELEMENT_FAIL;
			}
			else {
				this.status = Status.getStatus(value);
			}
		}
		else {
			return MODELELEMENT_FAIL;
		}
		
		return MODELELEMENT_OK;
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}

}
